import { createRouter, createWebHashHistory } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/login/LoginIndex.vue'),
    },
    {
      path: '/',
      component: () => import('@/views/home/HomeIndex.vue'),
      children: [
        { path: '/chat', component: () => import('@/views/second/ChatIndex.vue') },
        { path: '/my', component: () => import('@/views/second/MyIndex.vue') },
      ],
    },
  ],
})

//路由前置守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      ElMessage.error('请先登录')
      next('/login')
    }
  }
})

export default router
